﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Web;
using System.Diagnostics;
using System.IO;

namespace PingYou.ADO
{
    public class Common
    {
        #region public static string GetXmlValue(string key) 获取WebConfig的Key值

        private static string _WebConfigurePath = string.Empty;
        /// <summary>
        /// 配置文档路径
        /// </summary>
        public static string WebConfigurePath
        {
            get { return _WebConfigurePath.Length > 0 ? _WebConfigurePath : AppDomain.CurrentDomain.BaseDirectory + @"\Web.Config"; }
            set { _WebConfigurePath = value; }
        }

        /// <summary>
        /// 获取Xml的Key值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetXmlValue(string key)
        {
            string result = string.Empty;
            XmlDocument xd = new XmlDocument();
            XmlNodeList myXmlNodeList = null;
            try
            {
                xd.Load(WebConfigurePath);
                myXmlNodeList = xd.SelectSingleNode("configuration").SelectSingleNode("appSettings").SelectNodes("add");
            }
            catch
            {
                return "http://" + HttpContext.Current.Request.Url.Authority;
            }

            foreach (XmlElement xel in myXmlNodeList)
            {
                if (xel.GetAttribute("key") == key) { result = xel.GetAttribute("value"); break; }
            }
            return result;
        }
        #endregion

        #region public static void WriteLogErr(string myErrEx) 系统错误日志
        /// <summary>
        /// 系统错误日志
        /// </summary>
        /// <param name="myEx"></param>
        public static void WriteLogErr(string myErrEx)
        {
            //depth所请求的堆栈帧的索引，在获得所调用的方法0，获得此方法的方法1，获得调用此方法的方法的方法2。。。类推
            //MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();
            try
            {
                StackTrace st = new StackTrace();
                string methodName = st.GetFrame(1).GetMethod().Name;
                string className = st.GetFrame(1).GetMethod().DeclaringType.ToString();

                FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"LogErr.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine("");
                m_streamWriter.WriteLine("类名：" + className + "; 方法：" + methodName + "; 时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss[.fff] ") + " 错误信息：" + myErrEx);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
            catch
            {
                //Common.WriteLog(myEx.Message + ", StackTrace:" + myEx.StackTrace);
            }
        }
        #endregion

        #region public static void WriteLogErr(string myErrEx) 系统错误日志
        /// <summary>
        /// 系统错误日志
        /// </summary>
        /// <param name="myEx"></param>
        public static void WriteLogCJErr(string myErrEx)
        {
            //depth所请求的堆栈帧的索引，在获得所调用的方法0，获得此方法的方法1，获得调用此方法的方法的方法2。。。类推
            //MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();
            try
            {
                StackTrace st = new StackTrace();
                string methodName = st.GetFrame(1).GetMethod().Name;
                string className = st.GetFrame(1).GetMethod().DeclaringType.ToString();

                FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"LogCJErr.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine("");
                m_streamWriter.WriteLine("类名：" + className + "; 方法：" + methodName + "; 时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss[.fff] ") + " 错误信息：" + myErrEx);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
            catch (Exception myEx)
            {
                //Common.WriteLog(myEx.Message + ", StackTrace:" + myEx.StackTrace);
            }
        }
        #endregion
    }
}
